/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.db.impl;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.CostItem;
import com.yunhoo.base.model.MsgModel;
import com.yunhoo.sys.db.util.IbatisTemplate;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class CostItemImpl {

    private static SqlMapClient sqlMapClient = IbatisTemplate.getSqlMapClient();
    private static String nameSpace = "cos.";

    public static List<CostItem> geteconomic() {
        List<CostItem> list = new ArrayList();
        try {
            sqlMapClient.startTransaction();
            list = sqlMapClient.queryForList(nameSpace + "getcostitem",new CostItem());
            sqlMapClient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询错误geteoc~~");
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List getsubeconomic(CostItem dept) {
        List<CostItem> list = new ArrayList();
        try {
            sqlMapClient.startTransaction();
            list = sqlMapClient.queryForList(nameSpace + "getsubcostitem", dept);
            sqlMapClient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询错误getsubeoc~~");
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List getEconomicChild(String costcode) {
        List<CostItem> list = new ArrayList();
        try {
            sqlMapClient.startTransaction();
            
            CostItem param = new CostItem();
            param.setCode(costcode);
            list = sqlMapClient.queryForList(nameSpace + "getcostitemchild", param);
            sqlMapClient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询错误1~getEocChild~");
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List<CostItem> checkDepartmentRepeat(CostItem dept) {
        List<CostItem> list = null;
        try {
            sqlMapClient.startTransaction();
            System.out.println("准备查询是否重复");    
            list = sqlMapClient.queryForList(nameSpace + "checkCostRepeat", dept);
            for(CostItem a:list){           
            System.out.println("检查结果list.size:"+list.size());
            }
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static CostItem checkDepartmentParent(CostItem dept) {
        CostItem parent = null;
        try {
            sqlMapClient.startTransaction();
            System.out.println("准备查询父节点");
            parent = (CostItem) sqlMapClient.queryForObject(nameSpace + "checkCostParent", dept);
            if (parent != null) {                
            } else {
                System.out.println("parent is null");
            }
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return parent;
    }

    public static List<CostItem> checkEconomicRepeatForUpdate(CostItem dept) {
        List<CostItem> list = null;
        try {
            sqlMapClient.startTransaction();
            list = sqlMapClient.queryForList(nameSpace + "checkCostRepeatForUpdate", dept);
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void updateDepartment(CostItem dept) {
        try {
            sqlMapClient.startTransaction();
            sqlMapClient.update(nameSpace + "updateCost", dept);
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("updateCost~~");
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


        public static void updateDeptIsEnd(CostItem dept) {
        try {
            sqlMapClient.startTransaction();
            sqlMapClient.update(nameSpace + "updateCostIsEnd", dept);
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("updateCostIsEnd~~");
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

          public static void insertDept(CostItem dept) {
        try {
            sqlMapClient.startTransaction();
            sqlMapClient.insert(nameSpace + "insertCost", dept);
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("insertCost~~");
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<CostItem> getDeptSameLevel(CostItem dept) {
        List<CostItem> list = new ArrayList();
        try {
            sqlMapClient.startTransaction();
            list = sqlMapClient.queryForList(nameSpace + "getCostSameLevel", dept);
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void deleteDepartment(CostItem dept) {
        try {
            sqlMapClient.startTransaction();
            sqlMapClient.delete(nameSpace + "deleteCost", dept);
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("deleteCost~~");
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getCodeRule(){
        return (String)IbatisTemplate.query(nameSpace + "getCostGradeLevel", new CostItem());
    }

  public static MsgModel selectCostMsg(String code){
      CostItem param = new CostItem();
      param.setCode(code);
      return (MsgModel) IbatisTemplate.query(nameSpace + "selectCostMsg",param);
  }
   

    public static void testgetconomi() {
        List<CostItem> list = geteconomic();
        System.out.println("geteconomi测试:" + list.size());
    }

    public static void testgetsubeconomic() {
    }
//* * end * *
}
